package com.twt.recruit.service;


import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.twt.recruit.entity.Club;
import com.twt.recruit.entity.ClubMember;
import com.twt.recruit.mapper.ClubMapper;
import com.twt.recruit.mapper.ClubMemberMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Service
public class UserService {
    @Resource
    ClubMemberMapper clubMemberMapper;

    @Resource
    ClubMapper clubMapper;

    public JSONArray getUserOrgans(String uid){
        JSONArray re = new JSONArray();
        HashMap<String, Object> p = new HashMap<>();
        p.put("uid", uid);
        return getObjects(re, p);
    }

    public JSONArray getUserOrgans(String uid, Integer clubId){
        JSONArray re = new JSONArray();
        HashMap<String, Object> p = new HashMap<>();
        p.put("uid", uid);
        p.put("club_id", clubId);
        return getObjects(re, p);
    }

    private JSONArray getObjects(JSONArray re, HashMap<String, Object> p) {
        List<ClubMember> clubMembers = clubMemberMapper.selectByMap(p);
        if (!clubMembers.isEmpty()){
            for (ClubMember c : clubMembers) {
                JSONObject tmp = new JSONObject();
                Club club = clubMapper.selectById(c.getClubId());
                tmp.putOpt("clubId", c.getClubId());
                tmp.putOpt("name", club.getName());
                tmp.putOpt("scale", club.getScale());
                tmp.putOpt("role", c.getRole());
                tmp.putOpt("avatar", club.getAvatar());
                re.add(tmp);

            }
        }
        return re;
    }
}
